<?php

namespace App\Admin\Controllers\Posts;

use App\Admin\Repositories\Posts\WxPostsCommentManual;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;

class WxPostsCommentManualController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxPostsCommentManual(), function (Grid $grid) {
            $grid->column('id')->sortable();

            $grid->column('owers', '头像')->display(function ($v) {
                if($this->ower['user_avatar'] ?? '') {
                    return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png')) . '"  style="border-radius:50px;width:30px;" />';
                }
                return '';
            })->width('50px');
            $grid->column('ower')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('/users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '';
            })->width('100px');

            $grid->column('posts_id')->display(function ($v){
                if($v){
                    return '<a href="'.admin_url('posts/index').'" target="_blank">笔记'.$v.'</a>';
                }
                return '';
            });

            $grid->column('content');
            $grid->column('img');
            $grid->column('expect_time');
            $grid->column('publish_time');
            $grid->column('status')->using([0=>'待发布', 1=>'已发布'])->label([0=>'gray', 1=>'primary']);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxPostsCommentManual(), function (Show $show) {
            $show->field('id');
            $show->field('posts_id');
            $show->field('expect_user');
            $show->field('content');
            $show->field('img');
            $show->field('expect_time');
            $show->field('publish_time');
            $show->field('status');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxPostsCommentManual(), function (Form $form) {
            $form->display('id');
            $form->text('posts_id')->default(Cache::get('posts_comment_manual_last_post_id', ''))->required();
            $form->select('expect_user', '用户')->options(function ($id) {
                if($id){
                    $user = \App\Models\User\WxUser::find($id);
                    if ($user) {
                        return [$user->id => $user->user_name];
                    }
                }
            })->ajax('select/users');
            $form->textarea('content')->required();
            $form->image('img')->url('files/uploads')->autoUpload();
            $expect_time = \App\Models\Posts\WxPostsCommentManual::where('status', 0)->max('expect_time');
            if(_empty_($expect_time)){
                $expect_time = current_time();
            }else{
                $expect_time = Carbon::parse($expect_time);
                $now = Carbon::now();
                // 比较时间
                if ($expect_time->lessThan($now)) {
                    $expect_time = $now;
                }else{
                    $expect_time = Carbon::parse($expect_time)->addMinutes(mini_rand(1, 20));
                }
            }
            $form->datetime('expect_time')->default($expect_time)->required();
//            $form->text('publish_time');
//            $form->text('status');
        });
    }
}
